package com.wcm.system.service.shehuikexueyuantushu;

import com.wcm.system.model.shehuikexueyuantushu.HtMenuEntity;
import com.wcm.system.repository.shehuikexueyuantushu.SChannelRepository;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SDataServiceImpl implements SDataService {
    public static int STATUS = 0;
    public static int synCkmStatus = 0;
    public static int rowNvm = 0;
    public static int cellNvm = 0;
    public static int index = 0;
    @Autowired
    private SChannelRepository channelRepository;
    public static HSSFWorkbook workbook = getHSSWordBook("栏目表");

    private static HSSFWorkbook getHSSWordBook(String sheetName) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        /*****************************************************************/
        HSSFSheet sheet = workbook.createSheet(sheetName);
        // 设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth(15);
        // 生成一个样式
        return workbook;
    }
    @Override
    public Map generate(HttpServletResponse response) throws IOException {
        Map map = new HashMap();
        OutputStream outputStream = response.getOutputStream();
        response.setContentType("multipart/form-data");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String fileName = formatter.format(new Date())+".xls";
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        return map;
    }

    @Override
    public boolean getTreeList(int word, int rowNvm1, int cellNvm,String condition) {

        int thisCellNvm = new Integer(cellNvm);
        List<HtMenuEntity> data = channelRepository.selectByParent(word);
        if (data != null && !data.isEmpty()) {
            try{
                HSSFSheet sheet = workbook.getSheet("栏目表");
                index++;//索引
                for (int i = 0;i<data.size();i++) {
                    //行
                    HtMenuEntity hsubjectLanmuWithBLOBs = data.get(i);
                    int thesaurusWithBLOBs = 0;

                    try{
                        thesaurusWithBLOBs = hsubjectLanmuWithBLOBs.getMenuId();
                    }catch (Exception e){}

                    String name =hsubjectLanmuWithBLOBs.getClassName();
                    try{
                        name = name+";"+hsubjectLanmuWithBLOBs.getMenuId();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    HSSFRow row = sheet.createRow(rowNvm);
                    HSSFCell cell = row.createCell(thisCellNvm);
                    HSSFRichTextString text = new HSSFRichTextString(name);
                    cell.setCellValue(text);
                    rowNvm++;
                    List<HtMenuEntity> hsubjectLanmu = channelRepository.selectByParent(thesaurusWithBLOBs);
                    if(hsubjectLanmu!=null&&!hsubjectLanmu.isEmpty()){
                        this.cellNvm = this.cellNvm+1;
                        if(this.cellNvm<200){
                            getTreeList(thesaurusWithBLOBs,1,this.cellNvm,condition);
                        }
                    }
                    this.cellNvm = thisCellNvm;
                }}catch (Exception e){e.printStackTrace();}
            return true;
        } else {
            return false;
        }
    }

    public static String returnUrl(double id){
        try {


            //新闻
            if(id==1695){
                return "http://news.cpd.com.cn/";
            }
            //图片
            if(id==3820){
                return "http://pic.cpd.com.cn/";
            }
            //女警
            if(id==2225408){
                return "http://policewomen.cpd.com.cn/";
            }
            //禁毒
            if(id==2004487){
                return "http://626.cpd.com.cn/";
            }
            //生活
            if(id==2629556){
                return "http://life.cpd.com.cn/";
            }
            //文化
            if(id==2403712){
                return "http://culture.cpd.com.cn/";
            }
            //大案要案
            if(id==154977){
                return "http://daan.cpd.com.cn/";
            }
            //治安
            if(id==0){
                return "http://zhian.cpd.com.cn/";
            }
            //智慧警务
            if(id==30135868){
                return "http://zhjw.cpd.com.cn/";
            }
            //法治
            if(id==0){
                return "http://fazhi.cpd.com.cn/";
            }
            //中国驾驶
            if(id==0){
                return "http://zgjs.cpd.com.cn/";
            }
            //监管在线
            if(id==2689562){
                return "http://jiansuo.cpd.com.cn/";
            }
            //环保
            if(id==0){
                return "http://huanbao.cpd.com.cn/";
            }
            //交通
            if(id==0){
                return "http://jt.cpd.com.cn/";
            }

            //边防
            if(id==2450451){
                return "http://bf.cpd.com.cn/";
            }
            //食药打假
            if(id==23499348){
                return "http://sydj.cpd.com.cn/";
            }
            //民生
            if(id==0){
                return "http://minsheng.cpd.com.cn/";
            }
            //经侦
            if(id==2220387){
                return "http://eci.cpd.com.cn/";
            }
            //消防
            if(id==2634817){
                return "http://xf119.cpd.com.cn/";
            }


            //警用装备
            if(id==0){
                return "http://jyzb.cpd.com.cn/";
            }
            //派出所
            if(id==2574871){
                return "http://police.cpd.com.cn/";
            }
            //投稿平台
            if(id==0){
                return "http://post.cpd.com.cn/index";
            } //公安报信箱(登陆)
            if(id==0){
                return "http://mail.cpd.com.cn/";
            }







        }catch (Exception e){}


        return null;

    }
}



